package com.sxhuayuan.parking.service.impl;

import com.sxhuayuan.parking.config.Platform;
import com.sxhuayuan.parking.plugin.invoice.InvoicePlugin;
import com.sxhuayuan.parking.plugin.parking.ParkingPlugin;
import com.sxhuayuan.parking.plugin.partnerpay.PartnerpayApiService;
import com.sxhuayuan.parking.plugin.storage.StoragePlugin;
import com.sxhuayuan.parking.service.PluginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Service - 插件
 * 
 */
@Service("pluginServiceImpl")
public class PluginServiceImpl implements PluginService {

	@Resource
	private List<ParkingPlugin> parkingPlugins = new ArrayList<>();
	@Resource
	private List<StoragePlugin> storagePlugins = new ArrayList<>();
	@Resource
	private List<PartnerpayApiService> partnerpayApis = new ArrayList<>();
	@Resource
	private List<InvoicePlugin> invoicePlugins = new ArrayList<>();

	@Resource
	private Map<String, ParkingPlugin> ParkingPluginMap = new HashMap<String, ParkingPlugin>();
	@Resource
	private Map<String, StoragePlugin> storagePluginMap = new HashMap<String, StoragePlugin>();
	@Resource
	private Map<String, PartnerpayApiService> partnerpayApiMap = new HashMap<>();
	@Resource
	private Map<String, InvoicePlugin> invoicePluginMap = new HashMap<>();



	@Override
	public List<ParkingPlugin> getParkingPlugins() {
		Collections.sort(parkingPlugins);
		return parkingPlugins;
	}

	@Override
	public List<StoragePlugin> getStoragePlugins() {
		Collections.sort(storagePlugins);
		return storagePlugins;
	}

	@Override
	public List<PartnerpayApiService> getPartnerpayApiService() {
		return partnerpayApis.stream().filter((e) -> e.isEnabled()).sorted().collect(Collectors.toList());
	}

	@Override
	public PartnerpayApiService getPartnerpayApiService(Platform platform) {
		return partnerpayApiMap.get(platform.name() + "PartnerpayApiServiceImpl");
	}


	@Override
	public InvoicePlugin getInvoicePlugin(String id) {
		Collections.sort(invoicePlugins);
		return invoicePluginMap.get(id);
	}

	@Override
	public List<StoragePlugin> getStoragePlugins(final boolean isEnabled) {
		return storagePlugins.stream().filter((e) -> e.getIsEnabled() == isEnabled).sorted().collect(Collectors.toList());
	}

	@Override
	public List<InvoicePlugin> getInvoicePlugins() {
		Collections.sort(invoicePlugins);
		return invoicePlugins;
	}

	@Override
	public ParkingPlugin getParkingPlugin(String id) {
		return ParkingPluginMap.get(id);
	}

	@Override
	public StoragePlugin getStoragePlugin(String id) {
		return storagePluginMap.get(id);
	}

}